﻿using NHibernate;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using WebSite.Core;
using WebSite.Data.NH.Infrastructure;
using WebSite.Model;

namespace WebSite.Data.NH.Repositories
{
    public interface IRecruitmentRepository : IRepository<RecruitmentInfo>
    {
        /// <summary>
        /// 通过Id获取
        /// </summary>
        /// <param name="recId">Id</param>
        /// <returns>RecruitmentInfo</returns>
        RecruitmentInfo GetRec(int recId);

        /// <summary>
        /// 添加信息
        /// </summary>
        /// <param name="Rec">信息</param>
        /// <returns>True=操作成功/False=操作失败</returns>
        bool AddRec(RecruitmentInfo Rec);


        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="recId">Id</param>
        /// <returns>True=操作成功/False=操作失败</returns>
        bool DeleteRec(int recId);


        /// <summary>
        /// 更新信息
        /// </summary>
        /// <param name="Rec">信息</param>
        /// <returns>True=操作成功/False=操作失败</returns>
        bool UpdateRec(RecruitmentInfo Rec);


        /// <summary>
        /// 根据条件分页获得记录
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="orderByExpressions">排序字段</param>
        /// <returns>记录列表</returns>
        IList<RecruitmentInfo> LoadAll(Expression<Func<RecruitmentInfo, bool>> where,
                                                   int pageIndex,
                                                   int pageSize,
                                                   params IOrderByExpression<RecruitmentInfo>[] orderByExpressions);


        /// <summary>
        /// 获取总数
        /// </summary>
        /// <param name="where">条件</param>
        /// <returns>记录总数</returns>
        int LoadCount(Expression<Func<RecruitmentInfo, bool>> where);

    }

    public class RecruitmentRepository : NhRepositoryBase<RecruitmentInfo>, IRecruitmentRepository
    {
         private readonly IUnitOfWork uow;

         public RecruitmentRepository(ISession session, IUnitOfWork uow)
            : base(session)
        {
            this.uow = uow;
        }

        public RecruitmentInfo GetRec(int recId)
        {
            var result = GetById(recId);
            return result;
        }

        public bool AddRec(RecruitmentInfo Rec)
        {
            var result = Add(Rec);
            uow.Commit();
            return result;
        }

        public bool DeleteRec(int recId)
        {
            var Rec = GetById(recId);
            var result = Delete(Rec);
            uow.Commit();
            return result;
        }

        public bool UpdateRec(RecruitmentInfo Rec)
        {
            var result = Update(Rec);
            uow.Commit();
            return result;
        }

        public IList<RecruitmentInfo> LoadAll(Expression<Func<RecruitmentInfo, bool>> where, int pageIndex, int pageSize, params IOrderByExpression<RecruitmentInfo>[] orderByExpressions)
        {
            var query = GetMany(where, pageIndex, pageSize, orderByExpressions);
            return query.ToList();
        }

        public int LoadCount(Expression<Func<RecruitmentInfo, bool>> where)
        {
            var query = GetMany(where);
            return query.Count();
        }
    }
}
